﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.Core;
using ERP.Manager.DataEntity;
using ERP.Manager.DataProvider;
using ERP.Manager.DataHelper;
using System.Data.SqlClient;
using System.Data;
namespace ERP.Manager.SQLServerProvider
{
    public class ECS_goods_attrinfoRepository : IECS_goods_attrinfoRepository
    {
        ECS_attributeinfoRepository ecs_attributeinfoprepository = new ECS_attributeinfoRepository();

        #region 根据商品编号查询商品属性
        public List<ECS_goods_attrinfo> all_ecs_goods_attrinfo(int goods_id)
        {
            List<ECS_goods_attrinfo> list = new List<ECS_goods_attrinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_GOODS_ATTRINFO, new SqlParameter("@goods_id", goods_id));
            try
            {
                while (read.Read())
                {
                    ECS_goods_attrinfo goods_attr = new ECS_goods_attrinfo();
                    goods_attr.goods_attr_id = int.Parse(read["goods_attr_id"].ToString());
                    goods_attr.ecsgoodsinfo.goods_id = int.Parse(read["goods_id"].ToString());
                    goods_attr.ecsattributeinfo.attr_id = int.Parse(read["attr_id"].ToString());
                    goods_attr.attr_value = read["attr_value"].ToString();
                    goods_attr.attr_price = decimal.Parse(read["attr_price"].ToString());
                    ECS_attributeinfo ecs_attributeinfo = ecs_attributeinfoprepository.detail_ecs_attributeinfo(goods_attr.ecsattributeinfo.attr_id);
                    goods_attr.ecsattributeinfo.attr_name = ecs_attributeinfo.attr_name;
                    list.Add(goods_attr);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 添加商品类型属性
        public int add_ecs_goods_attrinfo(ECS_goods_attrinfo ecs_goods_attr)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_ECS_GOODS_ATTRIFO,
                new SqlParameter("@goods_id", ecs_goods_attr.ecsgoodsinfo.goods_id),
                new SqlParameter("@attr_id", ecs_goods_attr.ecsattributeinfo.attr_id),
                new SqlParameter("@attr_value", ecs_goods_attr.attr_value),
                new SqlParameter("@attr_price", ecs_goods_attr.attr_price));
            return rowcount;
        }
        #endregion

        #region 修改商品类型属性
        public int update_ecs_goods_attrinfo(ECS_goods_attrinfo ecs_goods_attr)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_ECS_GOODS_ATTRINFO,
                 new SqlParameter("@goods_id", ecs_goods_attr.ecsgoodsinfo.goods_id),
                 new SqlParameter("@attr_id", ecs_goods_attr.ecsattributeinfo.attr_id),
                 new SqlParameter("@attr_value", ecs_goods_attr.attr_value),
                 new SqlParameter("@attr_price", ecs_goods_attr.attr_price));
            return rowcount;
        }
        #endregion

        #region 删除商品类型属性
        public int delete_ecs_goods_attrinfo(int goods_id)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.DELETE_ECS_GOODS_ATTRINFO, new SqlParameter("@goods_id", goods_id));
            return rowcount;
        }
        #endregion
    }
}
